package org.example.multi_sys_consist.module;

import com.alibaba.fastjson.JSONObject;
import org.example.multi_sys_consist.constant.TransactionJobType;
import org.example.multi_sys_consist.pojo.entity.FinanceLog;
import org.example.multi_sys_consist.pojo.entity.TransactionJob;
import org.example.multi_sys_consist.service.FinanceLogService;
import org.example.multi_sys_consist.service.TransactionJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
public class CreateFinanceLogExecutor implements TransactionJobExecutor {
    @Autowired
    private FinanceLogService financeLogService;
    @Autowired
    private TransactionJobService transactionJobService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void execute(TransactionJob job) {
        JSONObject jo = JSONObject.parseObject(job.getData());
        List<FinanceLog> financeLog = financeLogService.createFinanceLog(jo.getLong("applyId"));

        //留下任务：推送财务日志
        JSONObject data = new JSONObject();
        data.put("applyId", jo.getLong("applyId"));
        transactionJobService.create(TransactionJobType.PUSH_FINANCE_LOG, data);
    }

    @Override
    public TransactionJobType type() {
        return TransactionJobType.CREATE_FINANCE_LOG;
    }
}
